var log = console.log;
// ajax_request('./main.php','POST',JSON,{data:123},(e)=>{console.log(e)})
function ajax_request(param,callback=new Function){
	// url,type,dataType="json",data=""
	var url = param["url"];
	var type = param["type"] ? param["type"] :"POST";
	var dataType = param["dataType"] ? param["dataType"] :"JSON";
	var data = param["data"] ? param["data"] :"";
	var access_token = param["access_token"] ? param["access_token"] :"";
	let ajax = 
		$.ajax({
			url: url ,
			type: type,
			dataType: dataType,
			data: data,
			beforeSend: function (request) {
				request.setRequestHeader("access_token",access_token);
			}
		}).done(function(response) {
			if(typeof response == "string"){
				if(JSON.parse(response)["errcode"] != undefined){
					if(check_ajax_request(JSON.parse(response)) == false){
						return;
					}
				}
			}else if(typeof response == "object"){
				if(response["errcode"] != undefined){
					if(check_ajax_request(response) == false){
						return;
					}
				}
			}
			
			callback(response);
		}).fail(function(response) {
			callback(response);
		});
}

function check_ajax_request(response){
	
	if(response["errcode"] >= 200 && response["errcode"] < 4000){
		swal({
			title: response["errmsg"],
			text: "请重新登录",
			type:  "error" ,
			dangerMode: true,
			allowOutsideClick:false 
		  })
		  .then(willDelete => {
			if (willDelete) {
			   window.location.href = "login.php";
			}
		  });
		  return false;
	}
	return true;
}

// 区域名称
function area_name(area_num){
    var text = "";
    if(area_num == "1"){
        text = "广佛版";
    }else if(area_num == "2"){
        text = "粤东版";
    }else if(area_num == "3"){
        text = "粤西版";
    }else if(area_num == "4"){
        text = "钦州版";
    }
    return text;
}

// alert_request('请选择一行',"")
function alert_request(title,text="",a_type=1,callback=new Function,type='error',is_show=1){
	is_show = !!is_show ;
	if(a_type===1){
		swal(title,text,type);
	}else{
		swal({ 
	      title: title , 
	      text: text, 
	      type:  type ,
	      showCancelButton: is_show, 
	      showConfirmButton: is_show, 
	      confirmButtonColor: '#3085d6',
	      cancelButtonColor: '#d33',
	      confirmButtonText: '确定',
	      cancelButtonText: '取消', 
	    }).then(function(result){
	    	if(result.value){
	    		callback(1);
	    	}else{
	    		callback(0);
	    	}
	    });
	}
}

// access_token 验证



